﻿using System;
using System.Management;
using System.Management.Automation;
using Microsoft.SharePoint;

namespace SPPoSh.Cmdlets.Artefacts.Items
{

    [Cmdlet(VerbsCommon.Get, "SPListItem")]
    public class GetSPListItemCmdlet : Cmdlet
    {
        [Parameter(Position = 0, Mandatory = true, HelpMessage = "URL of the web containing the list.")]
        public string Web;

        [Parameter(Position = 1, Mandatory = true, HelpMessage = "Name of the list.")]
        public string List;

        [Parameter(Position = 2, Mandatory = true, HelpMessage = "ID of the item in the list")]
        public int ItemID;

        protected override void ProcessRecord()
        {
            using (SPSite site = new SPSite(this.Web))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    SPList list = web.Lists[this.List];
                    SPListItem item = list.GetItemById(ItemID);
                    this.WriteObject(item, false);
                }
            }
        }
    }

    [Cmdlet(VerbsCommon.Remove, "SPListItem")]
    public class RemoveSPListItemCmdlet : Cmdlet
    {
        [Parameter(Position = 0, Mandatory = true, HelpMessage = "URL of the web containing the list.")]
        public string Web;

        [Parameter(Position = 1, Mandatory = true, HelpMessage = "Name of the list.")]
        public string List;

        [Parameter(Position = 2, Mandatory = true, HelpMessage = "ID of the item in the list")]
        public int ItemID;

        protected override void ProcessRecord()
        {
            using (SPSite site = new SPSite(this.Web))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    SPList list = web.Lists[this.List];
                    SPListItem item = list.GetItemById(ItemID);
                    item.Delete();
                    list.Update();
                }
            }
        }
    }
}
